package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.basic.Passport;
import cc.yugu.yhdw.common.domain.basic.PassportInfo;
import cc.yugu.yhdw.common.domain.hdw.Bicycle;
import cc.yugu.yhdw.common.domain.hdw.Supplier;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.persistence.basic.PassportMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.BicycleMapper;
import cc.yugu.yhdw.webserver.service.AbstractService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * 电动车业务
 * 
 * @Date:Created in 2018/7/5 19:32
 * @Author:
 * @Modified By: zjn 2018/7/5 19:32
 */ 
@Service
public class BicycleService extends AbstractService {
    @Autowired
    BicycleMapper bicycleMapper;
    @Autowired
    PassportMapper passportMapper;
    /**
    * 分页查询
    *
    * @Param: [bicycle]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/6  9:05
    */
    public Page findPage(Bicycle bicycle) {
        if (StringUtils.isNotEmpty(bicycle.getCustomerMobile())) {
            PassportInfo passport = passportMapper.findByMobile(bicycle.getCustomerMobile());
            if (passport != null) {
                bicycle.setCustomerId(passport.getId());
            }
        }

        Page page = bicycle.buildPage();
        page.setTotalItems(bicycleMapper.findPageCount(bicycle));
        bicycle.setBeginIndex(page.getOffset());

        List<Bicycle> bicycleList = bicycleMapper.findPageResult(bicycle);
        page.setResult(bicycleList);

        for (Bicycle e : bicycleList) {
            if (e.getCustomerId() != null) {
                PassportInfo passportInfo = findPassportInfo(e.getCustomerId());
                if (passportInfo != null) {
                    e.setCustomerName(passportInfo.getRealName());
                    e.setCustomerMobile(passportInfo.getMobile());
                }
            }
        }

        return page;
    }

    /**
    * 根据主键删除
    *
    * @Param: [id]
    * @Return: void
    * @Author By: zjn 2018/7/6  9:07
    */
    public void delete(String id) {
        bicycleMapper.delete(id);
    }
    /**
    * 根据客户id查询 1 条数据
    *
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.hdw.Bicycle
    * @Author By: zjn 2018/7/6  9:08
    */
    public Bicycle findByCustomer(Integer id) {
        return bicycleMapper.findByCustomer(id);
    }
    /**
    * 按主键查询
    *
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.hdw.Bicycle
    * @Author By: zjn 2018/7/6  9:08
    */
    public Bicycle find(String id) {
        Bicycle bicycle = bicycleMapper.find(id);
        if (bicycle.getCustomerId() != null) {
            PassportInfo passportInfo = findPassportInfo(bicycle.getCustomerId());
            if (passportInfo != null) {
                bicycle.setCustomerName(passportInfo.getRealName());
            }
        }
        return bicycle;
    }
}
